package com.heo.pojo;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import com.heo.pub.DictEnum;

public class ControlInfoEntity
{
  /**
   * 设备的IMEI号
   */
  private String deviceIMEI;

  /**
   * 设备的信号强度，默认0x00表示没有强度信息
   */
  private int signal = 0x00;
  /**
   * 设备类型
   */
  private Short deviceType;

  /**
   * 向设备发送命令的url地址
   */
  private String controlUrl;
  /**
   * 当前与该设备通讯的SocketChannelId
   */
  private String socketChannelId;

  /**
   * 设备的通道状态集合
   */
  private ConcurrentHashMap<Integer, ChannelInfo> channelInfoMap = new ConcurrentHashMap<Integer, ChannelInfo>();

  /**
   * 获得工作中的设备数量
   * 
   * @return
   */
  public Integer[] getBusyDeviceNum()
  {
    int busyDeviceNum = 0;// 工作中的通道数量
    int freeDeviceNum = 0;
    int errorDeviceNum = 0;
    if (channelInfoMap == null)
    {
      return new Integer[] { busyDeviceNum, freeDeviceNum, errorDeviceNum };
    }
    for (Integer key : channelInfoMap.keySet())
    {
      ChannelInfo channelInfo = channelInfoMap.get(key);
      if (channelInfo != null)
      {
        if (channelInfo.getChannelStatus() == DictEnum.DeviceChannelStatus.START_SUCCESS || channelInfo.getChannelStatus() == DictEnum.DeviceChannelStatus.WORKING || channelInfo.getChannelStatus() == DictEnum.DeviceChannelStatus.WORK_DONE || channelInfo.getChannelStatus() == DictEnum.DeviceChannelStatus.WORK_DONE)
        {
          busyDeviceNum++;
        }
        else if (channelInfo.getChannelStatus() == DictEnum.DeviceChannelStatus.FREE)
        {
          freeDeviceNum++;
        }
        else if (channelInfo.getChannelStatus() == DictEnum.DeviceChannelStatus.DEVICE_ERROR || channelInfo.getChannelStatus() == DictEnum.DeviceChannelStatus.DEVICE_CHANNEL_ERROR)
        {
          errorDeviceNum++;
        }
      }
    }
    return new Integer[] { busyDeviceNum, freeDeviceNum, errorDeviceNum };
  }

  public void addChannelInfo(Integer channelId, byte channelStatus)
  {
    ChannelInfo channelInfo = new ChannelInfo();
    channelInfo.setChannelId(channelId);
    channelInfo.setChannelStatus(channelStatus);
    channelInfo.setRefreshTime(new Date().getTime());
    channelInfoMap.put(channelId, channelInfo);
  }

  /**
   * 设置通道状态
   * 非原子操作，需要做同步
   * 
   * @param channelId
   * @param channelStatus
   */
  public synchronized void setChannelStatus(Integer channelId, byte channelStatus)
  {
    ChannelInfo channelInfo = channelInfoMap.get(channelId);
    if (channelInfo == null)
    {
      channelInfo = new ChannelInfo();
      channelInfo.setChannelId(channelId);
      channelInfo.setChannelStatus(channelStatus);
      channelInfo.setRefreshTime(new Date().getTime());
    }
    else
    {
      channelInfo.setChannelStatus(channelStatus);
      channelInfo.setRefreshTime(new Date().getTime());
    }
    channelInfoMap.put(channelId, channelInfo);
  }
  
  public String getChannelErrorCode(Integer channelId)
  {
    ChannelInfo channelInfo = channelInfoMap.get(channelId);
    if (channelInfo == null)
    {
      return null;
    }
    return channelInfo.getErrorCode();
  }
  
  public String getChannelErrorText(Integer channelId)
  {
    ChannelInfo channelInfo = channelInfoMap.get(channelId);
    if (channelInfo == null)
    {
      return null;
    }
    return channelInfo.getErrorText();
  }

  public Byte getChannelStatus(Integer channelId)
  {
    ChannelInfo channelInfo = channelInfoMap.get(channelId);
    if (channelInfo == null)
    {
      return null;
    }
    return channelInfo.getChannelStatus();
  }

  private static class ChannelInfo
  {
    /**
     * 设备的通道号
     */
    private Integer channelId;
    /**
     * 当前通道的工作状态
     */
    private byte channelStatus = DictEnum.DeviceChannelStatus.FREE;// 空闲状态
    
    private String errorCode;
    private String errorText;

    public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public String getErrorText() {
		return errorText;
	}

	public void setErrorText(String errorText) {
		this.errorText = errorText;
	}

	/**
     * 状态刷新的时间
     */
    private long refreshTime = 0;

    public Integer getChannelId()
    {
      return channelId;
    }

    public void setChannelId(Integer channelId)
    {
      this.channelId = channelId;
    }

    public byte getChannelStatus()
    {
      return channelStatus;
    }

    public void setChannelStatus(byte channelStatus)
    {
      this.channelStatus = channelStatus;
    }

    public long getRefreshTime()
    {
      return refreshTime;
    }

    public void setRefreshTime(long refreshTime)
    {
      this.refreshTime = refreshTime;
    }
  }

  public String getDeviceIMEI()
  {
    return deviceIMEI;
  }

  public void setDeviceIMEI(String deviceIMEI)
  {
    this.deviceIMEI = deviceIMEI;
  }

  public Short getDeviceType()
  {
    return deviceType;
  }

  public void setDeviceType(Short deviceType)
  {
    this.deviceType = deviceType;
  }

  public String getControlUrl()
  {
    return controlUrl;
  }

  public void setControlUrl(String controlUrl)
  {
    this.controlUrl = controlUrl;
  }

  public String getSocketChannelId()
  {
    return socketChannelId;
  }

  public void setSocketChannelId(String socketChannelId)
  {
    this.socketChannelId = socketChannelId;
  }

  public ConcurrentHashMap<Integer, ChannelInfo> getChannelInfoMap()
  {
    return channelInfoMap;
  }

  public void setChannelInfoMap(ConcurrentHashMap<Integer, ChannelInfo> channelInfoMap)
  {
    this.channelInfoMap = channelInfoMap;
  }

  public int getSignal()
  {
    return signal;
  }

  public void setSignal(int signal)
  {
    this.signal = signal;
  }

}
